package com.quanyan.ic.vendingMachine.http.controller;

import com.google.protobuf.Api;
import com.quanyan.ic.common.http.APIResponse;
import com.quanyan.ic.common.http.enums.ApiResponseEnum;
import com.quanyan.ic.vendingMachine.dao.VmPlaceMappingDAO;
import com.quanyan.ic.vendingMachine.http.request.ReqGoodsReplenishment;
import com.quanyan.ic.vendingMachine.http.request.ReqGridGoodsMapping;
import com.quanyan.ic.vendingMachine.http.request.ReqRsptRecordCondition;
import com.quanyan.ic.vendingMachine.http.request.ReqSalesRecordCondition;
import com.quanyan.ic.vendingMachine.http.service.VmAdminService;
import com.quanyan.ic.vendingMachine.model.InventoryWarningConfig;
import com.quanyan.ic.vendingMachine.model.VmPlaceMapping;
import com.quanyan.ic.vendingMachine.model.VmPlaceMappingExt;
import com.quanyan.ic.vendingMachine.model.VmUserPwd;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by zhuo.shi on 2017/7/26.
 */
@RestController
@RequestMapping("/larkic/vendingMachine/admin")
public class VmAdminController {

    @Autowired
    private VmAdminService vmAdminService;
    @Autowired
    private VmPlaceMappingDAO vmPlaceMappingDAO;

    @RequestMapping(value = "/vmCategoryList")
    public APIResponse vmCategoryList() {
        return APIResponse.returnSuccess(vmAdminService.vmCategoryList());
    }

    @RequestMapping(value = "/createPlaceVm", method = RequestMethod.POST)
    public APIResponse createPlaceVm(@RequestBody VmPlaceMapping vmPlaceMapping) {
        return APIResponse.returnSuccess(
                vmAdminService.createPlaceVm(vmPlaceMapping));
    }

    @RequestMapping(value = "/updatePlaceVm", method = RequestMethod.POST)
    public APIResponse updatePlaceVm(@RequestBody VmPlaceMappingExt ext) {
        boolean isSuccess = true;
        if (ext.getPlaceId() == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        //判断密码是不是唯一
        List<VmUserPwd>  userPwds = vmPlaceMappingDAO.findVmUserPwd(ext.getPlaceId());
        for(VmUserPwd userPwd :userPwds){
             if(userPwd.getPassword().equals(ext.getUserPwdList().get(0).getPassword())){
                 isSuccess = false;
             }
        }
        if(!isSuccess){
          return   APIResponse.returnFail(ApiResponseEnum.PWSWROD_HAS_EXISTS);
        }

        vmAdminService.updatePlaceVm(ext);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/getPlaceVmPoster", method = RequestMethod.POST)
    public APIResponse findPlaceVmPoster(@RequestBody Map<String, Object> map) {
        Integer placeId = MapUtils.getInteger(map, "placeId");
        if (placeId == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(vmAdminService.findVmPoster(placeId));
    }

    @RequestMapping(value = "/getPlaceVmUserPwd", method = RequestMethod.POST)
    public APIResponse getPlaceVmUserPwd(@RequestBody Map<String, Object> map) {
        Integer placeId = MapUtils.getInteger(map, "placeId");
        if (placeId == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(vmAdminService.findVmUserPwd(placeId));
    }

    @RequestMapping(value = "/getVmCustomerTel", method = RequestMethod.POST)
    public APIResponse getVmCustomerTel(@RequestBody Map<String, Object> map) {
        Integer placeId = MapUtils.getInteger(map, "placeId");
        String vmNumber = MapUtils.getString(map, "vmNumber");
        if (placeId == null && StringUtils.isEmpty(vmNumber)) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        if (placeId != null) {
            return APIResponse.returnSuccess(
                    vmAdminService.findVmCustomerTel(placeId));
        }
        if (!StringUtils.isEmpty(vmNumber)) {
            return APIResponse.returnSuccess(
                    vmAdminService.findVmCustomerTel(vmNumber));

        }
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/delVmPoster", method = RequestMethod.POST)
    public APIResponse delVmPoster(@RequestBody Map<String, Object> map) {
        Integer id = MapUtils.getInteger(map, "id");
        if (id == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        vmAdminService.deleteVmPoster(id);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/delVmUserPwd", method = RequestMethod.POST)
    public APIResponse delVmUserPwd(@RequestBody Map<String, Object> map) {
        Integer id = MapUtils.getInteger(map, "id");
        if (id == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        vmAdminService.deleteUserPwd(id);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/findPlaceVm", method = RequestMethod.POST)
    public APIResponse findPlaceVm(@RequestBody Map<String, Object> map) {
        String vmNumber = MapUtils.getString(map, "vmNumber");
        if (StringUtils.isEmpty(vmNumber)) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(
                vmAdminService.findPlaceVm(vmNumber));
    }

    @RequestMapping(value = "/validateVmNumber", method = RequestMethod.POST)
    public APIResponse validateVmNumber(@RequestBody Map<String, Object> map) {
        String vmNumber = MapUtils.getString(map, "vmNumber");
        if (StringUtils.isEmpty(vmNumber)) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(
                vmAdminService.validatePlaceVm(vmNumber));
    }

    @RequestMapping("/placeVmMappingList")
    public APIResponse placeVmMappingList(@RequestBody Map<String, Object> map) {
        Integer cityId = MapUtils.getInteger(map, "cityId");
        Integer placeId = MapUtils.getInteger(map, "placeId");
        return APIResponse.returnSuccess(
                vmAdminService.queryPlaceVmMappingList(cityId, placeId, MapUtils.getInteger(map, "pageNum"), MapUtils.getInteger(map, "pageSize"))
        );
    }

    @RequestMapping("/disablePlaceVmMapping")
    public APIResponse delPlaceVmMapping(@RequestBody Map<String, Object> map) {
        vmAdminService.disablePlaceVmMapping(MapUtils.getInteger(map, "id"));
        return APIResponse.returnSuccess();
    }

    @RequestMapping("/placeVmInfoList")
    public APIResponse placeVmInfoList(@RequestBody Map<String, Object> map) {
        Integer placeId = MapUtils.getInteger(map, "placeId");
        Boolean isShowInventory = MapUtils.getBoolean(map, "isShowInventory");
        isShowInventory = (isShowInventory == null ? false : isShowInventory);
        Boolean isCheckInventory = MapUtils.getBoolean(map, "isCheckInventory");
        isCheckInventory = (isCheckInventory == null ? false : isCheckInventory);
        return APIResponse.returnSuccess(
                vmAdminService.queryPlaceVmInfoList(placeId, isShowInventory, isCheckInventory));
    }

    @RequestMapping("/gridGoodsMapping")
    public APIResponse gridGoodsMapping(@RequestBody ReqGridGoodsMapping reqGridGoodsMapping) {
        vmAdminService.createGridGoodsMapping(
                reqGridGoodsMapping.getVmNumber(), reqGridGoodsMapping.getGridList(), reqGridGoodsMapping.getGoodsInfo());
        return APIResponse.returnSuccess();
    }

    @RequestMapping("/clearGridGoods")
    public APIResponse clearGridGoods(@RequestBody ReqGridGoodsMapping reqGridGoodsMapping) {
        vmAdminService.clearGridGoodsMapping(reqGridGoodsMapping.getVmNumber(), reqGridGoodsMapping.getGridList());
        return APIResponse.returnSuccess();
    }

    @RequestMapping("/setInventoryWarning")
    public APIResponse setInventoryWarning(@RequestBody InventoryWarningConfig config) {
        vmAdminService.setInventoryWarningConfig(config);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/getInventoryWarning", method = RequestMethod.POST)
    public APIResponse getInventoryWarning(@RequestBody Map<String, Object> map) {
        Integer placeId = MapUtils.getInteger(map, "placeId");
        return APIResponse.returnSuccess(vmAdminService.getInventoryWarningConfig(placeId));
    }

    @RequestMapping("/replenishment")
    public APIResponse replenishment(@RequestBody ReqGoodsReplenishment req) {
        if (StringUtils.isEmpty(req.getVmNumber()) || req.getUserPwd() == null
                || CollectionUtils.isEmpty(req.getRsptInfoList())) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        vmAdminService.goodsReplenishment(req.getVmNumber(), req.getRsptInfoList(), req.getUserPwd());
        return APIResponse.returnSuccess();
    }

    @RequestMapping("/replenishmentRecordList")
    public APIResponse replenishmentRecordList(@RequestBody ReqRsptRecordCondition condition) {
        return APIResponse.returnSuccess(vmAdminService.queryRsptRecordList(condition));
    }

    @RequestMapping("/salesRecordList")
    public APIResponse salesRecordList(@RequestBody ReqSalesRecordCondition condition) {
        return APIResponse.returnSuccess(vmAdminService.querySalesRecordList(condition));
    }

    @RequestMapping("/gridGoodsInfoList")
    public APIResponse gridGoodsInfoList(@RequestBody Map<String, String> map) {
        return APIResponse.returnSuccess(
                vmAdminService.findVendingMachine(MapUtils.getString(map, "vmNumber"), true, false));
    }
    @RequestMapping("/adminLogin")
    public APIResponse adminLoginMapping(@RequestBody Map<String,String> map){
        boolean hasPsw  = false;
        if(StringUtils.isEmpty(MapUtils.getString(map,"vmNumber")) || StringUtils.isEmpty(MapUtils.getString(map,"pswNumber"))){
                        return  APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        VmPlaceMapping vmPlaceMapping = vmPlaceMappingDAO.findByVmNumber(MapUtils.getString(map,"vmNumber"));
        if(vmPlaceMapping != null){
            List<VmUserPwd>  userPwds = vmPlaceMappingDAO.findVmUserPwd(vmPlaceMapping.getPlaceId());
            if(userPwds !=null && userPwds.size()>0){
                  for(VmUserPwd userPwd :userPwds){
                        if(userPwd.getPassword().equals(MapUtils.getString(map,"pswNumber"))){
                            hasPsw = true;
                        }
                  }
            }
        }
        return hasPsw ?  APIResponse.returnSuccess() : APIResponse.returnFail(ApiResponseEnum.ACCOUNT_OR_PASSWORD_ERROR);
    }
}
